package com.bhs.sansonglogistics.ui.enterprise;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.base.BaseBean;
import com.bhs.sansonglogistics.bean.Location;
import com.bhs.sansonglogistics.bean.PictureBean;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.adapter.PictureSelectAdapter;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.luck.picture.lib.entity.LocalMedia;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.util.SmartGlideImageLoader;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 运输保单
 */
public class TransportPolicyActivity extends BaseActivity implements NetCallBack {

    private RecyclerView mRvTransportPolicy;
    private PictureSelectAdapter adapter;
    private Button mBtnSubmit;

    @Override
    public int initLayout() {
        return R.layout.activity_transport_policy;
    }

    @Override
    public void initView() {
        TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("运输保单");
        mRvTransportPolicy = findViewById(R.id.rv_Transport_Policy);
        mBtnSubmit = findViewById(R.id.btn_submit);

        mBtnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(adapter.getImages())) {
                    ToastUtil.show("请选择照片");
                    return;
                }
                submit();
            }
        });
    }

    private void submit() {
        networkRequest(netApi.uploadInsurance(adapter.getImages()), this);
    }

    @Override
    public void initData() {
        adapter = new PictureSelectAdapter(9, this);
        mRvTransportPolicy.setAdapter(adapter);
        mRvTransportPolicy.setLayoutManager(new GridLayoutManager(this, 3));
        ArrayList<String> attachment = getIntent().getStringArrayListExtra("attachment");
        List<PictureBean> companyList=new ArrayList<>();
        for (String company_pic : attachment) {
            companyList.add(new PictureBean(company_pic,company_pic));
            adapter.localMedia.add(LocalMedia.generateHttpAsLocalMedia(MyUtils.watermark(company_pic)));
        }
        if (adapter.getMaxSelectNum() >companyList.size()) {
            companyList.add(new PictureBean(2));
        }
        adapter.setNewData(companyList);

    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        BaseBean bean = new Gson().fromJson(json, BaseBean.class);
        ToastUtil.show(bean.getMsg());
        if (bean.isStatus()) {
            finish();
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }
}